package com.itheima.activities.task;

import com.itheima.activities.service.RedPacketService;
import com.itheima.entity.pojo.RedPacket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author :wujunjie
 * createTime :2022-12-21 17:34
 * description :
 */
@Component
@Slf4j
public class ChangeRedPacketStatus {
    @Autowired
    private RedPacketService redPacketService;

    @Scheduled(fixedRate = 5*1000)
    @Transactional
    public void task(){
        List<RedPacket> redPackets =redPacketService.selectRedPacketByStatusAndTime();
        if (redPackets.size()==0){
            log.info("未发现需要上线的活动");
            return;
        }
        Set<String> ids = redPackets.stream().map(RedPacket::getId).collect(Collectors.toSet());
        Boolean updateRedPacketStatus=redPacketService.updateRedPacketStatus(ids,1);
        if (!updateRedPacketStatus){
            throw new RuntimeException("更新活动失败");
        }
    }
}
